package com.sanniu.common.sms;

import com.jfinal.aop.Inject;
import com.sanniu.common.utils.CommonsUtils;
import com.sanniu.model.BaseRet;
import com.sanniu.model.SmsVcodeRecord;
import com.sanniu.service.SmsVcodeRecordService;
import com.sanniu.service.provider.SmsVcodeRecordServiceProvider;
import io.jboot.Jboot;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SmsKit {

    private static final String CACHE_NAME = "sms_code";
    private static SmsVcodeRecordService smsVcodeRecordService = new SmsVcodeRecordServiceProvider();
    private static final int dayMaxSendNum = 10;//每天发送10次
    private static final int vCodeIndate = 2;//2个小时

    /**
     * 发送短信
     *
     * @param mobile   手机号
     * @param template 短信模板
     * @param sign     短信签名
     * @return
     */
    public static boolean sendSms(String mobile, String template, String sign) {
        SmsMessage sms = new SmsMessage();
        sms.setSign(sign);
        sms.setMobile(mobile);
        sms.setTemplate(template);
        return sms.send();
    }

    private static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1);//+1今天的时间加一天
        date = calendar.getTime();
        return date;
    }

    /**
     * 发送短信验证码
     *
     * @param mobile
     * @param code
     * @param template
     * @param sign
     * @return
     */
    public static BaseRet sendCode(String mobile, String code, String template, String sign) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = new Date();
        String dateNowStr = sdf.format(d);
        String nextDayStr = sdf.format(getNextDay(d));
        Date beginTime = null;
        Date endTime = null;
        try {
            beginTime = sdf.parse(dateNowStr);
            endTime = sdf.parse(nextDayStr);
            System.out.println(beginTime);
            System.out.println(endTime);
        } catch (Exception exp) {
            System.out.println(exp);
            return BaseRet.message("发送异常" + exp.toString());
        }
        System.out.println(smsVcodeRecordService);
        if (smsVcodeRecordService.findCountByMobile(mobile, beginTime, endTime) >= dayMaxSendNum) {
            return BaseRet.message("超出每日每手机号" + dayMaxSendNum + "条发送限制");
        }

        SmsMessage sms = new SmsMessage();
        sms.setCode(code);
        sms.setSign(sign);
        sms.setMobile(mobile);
        sms.setTemplate(template);

        if (sms.send()) {
            SmsVcodeRecord record = new SmsVcodeRecord();
            record.setCode(code);
            record.setMobile(mobile);
            record.setCreateTime(new Date());
            record.setState(1);
            record.setType(1);
            smsVcodeRecordService.save(record);
            //有效期，2个小时
            Jboot.getCache().put(CACHE_NAME, mobile, code, 60 * 60 * vCodeIndate);
            return BaseRet.data(null);
        }
        return BaseRet.message("发送失败");
    }

    /**
     * 验证用户输入的手机号是否正确
     *
     * @param mobile
     * @param code
     * @return
     */
    public static boolean validateCode(String mobile, String code) {
        String cacheCode = Jboot.getCache().get(CACHE_NAME, mobile);
        return cacheCode != null && cacheCode.equals(code);
    }


    /**
     * 生成一个四位数字的码
     *
     * @return
     */
    public static String generateCode() {
        return CommonsUtils.generateCode();
    }

}
